/**
 * 
 */
package com.zznode.client.wlanpolling.test;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.model.GeneralInfo;
import com.zznode.client.wlanpolling.model.WLanInfo;
import com.zznode.client.wlanpolling.service.Wifi;

/**
 * @author Jerome
 * 资源确认页面
 */
public class ResConfirmAdapter extends BaseExpandableListAdapter {

	private LayoutInflater mInflater = null;
	
	// WlanInfo
	private WLanInfo wlanInfo;
	
	public ResConfirmAdapter(Activity ctx, Wifi wifi) {
		super();
		mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		wlanInfo = new WLanInfo(wifi);
	}
	
	public void resetWLanInfo(WLanInfo wlanInfo){
		this.wlanInfo = wlanInfo;
		this.notifyDataSetChanged();
	}
	
	public WLanInfo getWLanInfo(){
		return wlanInfo;
	}

	@Override
	public int getGroupCount() {
		// AP信息，热点信息，上联信息
		return wlanInfo.allInfo.length;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return wlanInfo.allInfo[groupPosition];
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return getGroup(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		View view = convertView;
        if(view == null){
            view = mInflater.inflate( R.layout.rescfmgrp , null);
        }
        TextView grpName = (TextView) view.findViewById(R.id._grpname);
        String name = wlanInfo.allInfo[groupPosition].getGroupName();
        grpName.setText(name);
        
        ImageView groupIndicator = (ImageView) view.findViewById(R.id.group_indicator);
        if(!isExpanded) {
        	groupIndicator.setImageResource(R.drawable.icon_close);
        } else {
        	groupIndicator.setImageResource(R.drawable.icon_open);
        }
        
        return view;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		GeneralInfo info = wlanInfo.allInfo[groupPosition];
		int layoutID = info.getLayoutID();        
        int rootID = info.getRootID();
		
        if (convertView == null || rootID != convertView.getId()) {
			convertView = mInflater.inflate(layoutID, null);
        }
		info.bind(convertView);     
        return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
